#Create a simulator object
set ns [new Simulator]
#set f [open out.tr w]
#$ns trace-all $f


#Define a 'finish' procedure
proc finish {} {

        global ns
        exit 0
}

# calculate throughput
proc throughput {monitor start end name} {
    global ns
    set bytes [$monitor set bytes_]
    set tp [expr ( $bytes * 8.0 ) / ( $end - $start )]
    set now [$ns now]

    puts "$name: Throughput: $tp b/s"
}

proc connect {nodeA nodeB bandwidth} {

    global ns
    $ns duplex-link $nodeA $nodeB $bandwidth 2ms DropTail    
}

#rate is in Mb/s
# M = 1024*1024
# b = 1B / 8
proc add_exp { src dest} {

    global ns

    set udp [new Agent/UDP]
    $ns attach-agent $src $udp

    set traffic [new Application/Traffic/Exponential]
    $traffic set packetSize_ 210
    $traffic set burst_time_ 500ms
    $traffic set idle_time_ 500ms
    $traffic set rate_ 100*1024
        
    $traffic attach-agent $udp

    set lm [new Agent/LossMonitor]
    $ns attach-agent $dest $lm

    $ns connect $udp $lm

    return [list $traffic $lm]
}

proc add_pareto { src dest } {

    global ns

    set udp [new Agent/UDP]
    $ns attach-agent $src $udp

    set p [new Application/Traffic/Pareto]
    $p set burst_time_ 500ms
    $p set idle_time_ 500ms
    $p set rate_ 100*1024
    $p set packetSize_ 210
    $p set shape_ 1.5
        
    $p attach-agent $udp

    set lm [new Agent/LossMonitor]
    $ns attach-agent $dest $lm

    $ns connect $udp $lm

    return [list $p $lm]
}

#Add logic here

#Create nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

set r0 [$ns node]
set r1 [$ns node]

#Connect then
connect $n0 $r0 10Mb
connect $n1 $r0 10Mb

connect $n2 $r1 10Mb
connect $n3 $r1 10Mb

connect $r0 $r1 1Mb

#add cbr
set result [add_exp $n0 $n2]
set expo02  [lindex $result 0]
set lm02   [lindex $result 1]

set result [add_pareto $n1 $n3]
set par13  [lindex $result 0]
set lm13   [lindex $result 1]

set start 1
set end   51

#send and stop
$ns at $start "$expo02 start"
$ns at $start "$par13 start"
$ns at $end   "$par13 stop"
$ns at $end   "$expo02 stop"

#Define end time
$ns at [expr $end + 1] "throughput $lm02 $start $end Node0_Node2_Expo"
$ns at [expr $end + 1] "throughput $lm13 $start $end Node1_Node3_Pareto"
$ns at [expr $end + 1] "finish"

#Run the simulation
$ns run